<?php

/**
 * Api基于无状态接口 不能使用session 保存数据
 * Class ApiAction
 */
class ApiAction extends BaseApiAction
{
    /**
     * 用户登录
     */
    function login()
    {
        try {
            $post = I('post.');
            $pwd = sprintf($this::pwd_str_format, $post['user_name'], $post['password']);
            $md5 = md5($pwd);
            $user = D('source_client')
                ->field('id,pwd')
                ->where(['user_name' => $post['user_name'], 'status' => 1])
                ->find();

            if ($user['pwd'] === $md5) {

                $des = new Des('Q!W@E#R$');

                $auth = $des->encrypt(json_encode(['user_id' => $user['id'], 'time' => time()]));
                $data = D("source_client")->field("*")->where(['id' => $user['id']])->find();

                $this->callback(["result" => $data], ['auth' => $auth]);
            } else {
                $this->callback(null, null, '用户名或密码错误', -1);
            }
        } catch (Exception $ex) {
            $this->callback(null, null, '服务器繁忙', -1);
        }
    }

    /**
     * 用户头像
     */
    function userHead()
    {
        $post = I('post.');
        $status = 0;
        if ($post['account']) {
            $draw = D(JH_SOURCE_CLIENT);
            $data['account'] = $post['account'];
            $data['img'] = $post['img'];

            $id = $draw->where(['account' => $post['account'], 'status' => 1])->getField('id');

            if ($id > 0) {
                // 创建目录
                createdir(APP_PATH . '/Source/scdc/header/' . $id);
                // 保存图片
                $SavePath = clean_path(APP_PATH . '/Source/scdc/header/');
                $t_img = $id . '/' . date('YmdHis') . '.jpg';
                $img = $SavePath . $t_img;
                $img_res = file_put_contents($img, base64_decode($post['img']));
                if ($img_res !== false) {
                    $arr['img'] = $t_img;
                    $arr['u_time'] = time();
                    $res = $draw->where(['id' => $id])->save($arr);
                    if ($res !== false) {
                        $status = 1;
                    }
                }
            }
        }

        $this->callback(["status" => $status]);
    }


    /**
     * 文章列表
     */
    function art_list()
    {
//        $_POST['where']['status'] = 1;
//        $data = D(JH_ARTICLE)->field("id,title,img,url")->order('id desc')->where(['status' => 1])->select();
//        foreach ($data as $key => $val) {
//            $data[$key]['img'] = $val['img'] ? C('img_url') . 'article/' . $val['img'] : C('img_url') . 'article/default.png';
//            $data[$key]['url'] = $val['url'] ? $val['url'] : C('host') . 'scdc/index/article?id=' . $val['id'];
//        }
//        $this->callback(["result" => ['data' => $data]]);

        $_POST['where']['status'] = 1;
        $data = $this->default_post2(D(JH_ARTICLE)->field("id,title,img,url"));
        foreach ($data['data'] as $key => $val) {
            $data['data'][$key]['img'] = $val['img'] ? C('img_url') . 'article/' . $val['img'] : C('img_url') . 'article/default.png';
            $data['data'][$key]['url'] = $val['url'] ? $val['url'] : C('host') . 'scdc/index/article?id=' . $val['id'];
        }
        $this->callback(["result" => $data]);

    }

    /**
     * 广告
     */
    function banner()
    {
        $_POST['where']['status'] = 1;
        $_POST['order'] = 'sort desc, id desc';
        $_POST['length'] = $_POST['length'] ? $_POST['length'] : 5;

        $data = $this->default_post(D(JH_BANNER)->field("id,title,img,status"));
        foreach ($data as $key => $val) {
            $data[$key]['img'] = $val['img'] ? C('img_url') . 'banner/' . $val['img'] : '';
        }
        $this->callback(["result" => $data]);
    }

    /**
     * apk更新
     */
    function update()
    {
        $data = D(JH_UPDATE)->field("id,name,version,url,remark")->order('id desc')->find();
        $data['url'] = C('apk') . $data['url'];
        $this->callback(["result" => $data]);
    }

    /**
     * 访客记录
     */
    function visitor()
    {
        $post = I('post.');

        $status = 0;
        if ($post['name'] && $post['phone']) {
            $data['name'] = $post['name'];
            $data['phone'] = $post['phone'];
            $data['work_unit'] = $post['work_unit'];
            $data['floor'] = $post['floor'];
            $data['user_name'] = $post['user_name'];
            $data['date'] = $post['date'];
            $data['time'] = $post['time'];

            $result = D(JH_VISITOR)->add(array(
                "name" => $post['name'],
                "phone" => $post['phone'],
                "work_unit" => $post['work_unit'],
                "floor" => $post['floor'],
                "user_name" => $post['user_name'],
                "date" => $post['date'],
                "time" => $post['time'],
                "v_time" => time(),
            ));
            if ($result !== false) {
                $status = 1;
            }

            $this->callback(["status" => $status]);
        }
    }

    /**
     * 添加意见反馈
     */
    function feedback()
    {
        $post = I('post.');
        $status = 0;
        if ($post['name'] && $post['account']) {
            $data['name'] = $post['name'];
            $data['account'] = $post['account'];
            $data['message'] = $post['message'];

            $result = D(JH_FEEDBACK)->add(array(
                "name" => $post['name'],
                "account" => $post['account'],
                "message" => $post['message'],
                "msg_type" => 1,
                "time" => time(),
            ));
            if ($result !== false) {
                $status = 1;
            }
            $this->callback(["status" => $status]);
        }
    }

    /**
     * 发送意见反馈
     */
    function feedback_client()
    {
        $post = I('post.');

        $_POST['where']['time'] = array('gt', strtotime($post['time']));

        $_POST['where']['account'] = $post['account'];

        $_POST['order'] = 'id asc';

        $data = $this->default_post(D(JH_FEEDBACK)->field('id,account,message,msg_type,time'));

        foreach ($data as $k => $v) {
            $data[$k]["time"] = date('Y-m-d H:i:s', $v["time"]);
        }

        $this->callback(["result" => $data]);

    }

    /**
     * 修改密码
     */
    function changePwd()
    {
        try {
            $post = I('post.');
            $status = 0;
//        print_r($post);
            if ($post['uid'] && $post['old_pwd'] && $post['new_pwd']) {
                $data = D(JH_SOURCE_CLIENT)->field("*")->where(['uid' => $post['uid']])->find();
                $user_name = $data['user_name'];

                $pwd = md5(sprintf($this::pwd_str_format, $user_name, $post['old_pwd']));

                if ($pwd == $data['pwd']) {
                    $new_pwd = md5(sprintf($this::pwd_str_format, $user_name, $post['new_pwd']));

                    $arr = array(
                        "id" => $data['id'],
                        'pwd' => $new_pwd
                    );
                    D(JH_SOURCE_CLIENT)->save($arr);

                    $this->callback(["result" => $status]);
                } else {
                    $this->callback(["result" => 1]);
                }
            }
        } catch (Exception $ex) {
            $this->callback(null, null, '服务器繁忙', -1);
        }
    }

    /**
     * 判断是否上传市场
     */
    function isMarketCheck()
    {
        try {
            $data = D(market_check)->where("id=1")->find();

            $this->callback(["result" => $data]);
        } catch (Exception $ex) {
            $this->callback(null, null, '服务器繁忙', -1);
        }
    }
}
